package com.xiaoluo.deploy;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.internal.LinkedTreeMap;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.squareup.okhttp.*;
import org.codehaus.jettison.json.JSONObject;

import java.io.File;
import java.net.URL;
import java.util.List;

/**
 * Project : DeployTest
 * Package : com.xiaoluo.deploy
 * Created by Willian.
 * Date  : 2016/3/3 20:27.
 */
public class SettingAction extends AnAction {
    ConfigureDialog dialog = new ConfigureDialog();

    @Override
    public void actionPerformed(AnActionEvent anActionEvent) {
        Project project = anActionEvent.getData(PlatformDataKeys.PROJECT);



        getPods();

        dialog.pack();
        dialog.setProject(project);
        dialog.loadProperties();
        dialog.setTitle(project.getName()+" Deploy...");
        dialog.getLoadField().setVisible(false);
        dialog.getProjName().setText(project.getBasePath());

        if(dialog.getBounds().getX()<1){
            dialog.setBounds(100,100,800,450);
        }else {
            dialog.setBounds(dialog.getBounds().x,dialog.getBounds().y,800,450);
        }

        dialog.setVisible(true);
    }

    public void getPods(){


        OkHttpClient client = new OkHttpClient();

        try {
            URL url = new URL("http://192.168.1.168:28080/xl-cd-api/getPods");

            Request.Builder httpRequestBuild =  new Request.Builder();
            httpRequestBuild.url(url);
            httpRequestBuild.get();
            Request request =  httpRequestBuild.build();
            Response response = client.newCall(request).execute();
            String jsonStr = response.body().string();
            System.out.println(jsonStr);


            Gson gson = new Gson();
            ResultForm result = gson.fromJson(jsonStr, ResultForm.class);

            if(result != null && result.getCode()==0 && result.getData() != null){
                dialog.setPods((List<LinkedTreeMap>) result.getData());
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            // Clean up.
        }
    }

}
